package com.dragonboat.controller;
import com.dragonboat.util.ResponseResult;import com.dragonboat.entity.Event;
import com.dragonboat.entity.Organizer;
import com.dragonboat.entity.Registration;
import com.dragonboat.service.OrganizerService;
import com.dragonboat.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import java.util.List;
import com.dragonboat.repository.OrganizerRepository;
@RestController
@RequestMapping("/organizer")
public class OrganizerController {
    @Autowired
    private OrganizerService organizerService;
    @Autowired
    private OrganizerRepository organizerRepository;
    @PostMapping("/register")
    public ResponseResult<Organizer> register(@RequestBody Map<String, String> request) {
        String organizerName = request.get("organizerName");
        String password = request.get("password");

        if (organizerRepository.existsByOrganizerName(organizerName)) {
            return ResponseResult.error("主办方名称已存在");
        }

        Organizer organizer = new Organizer();
        organizer.setOrganizerName(organizerName);
        organizer.setPassword(PasswordUtil.encode(password)); // 使用哈希密码

        organizerRepository.save(organizer);
        organizer.setPassword(null); // 不返回密码

        return ResponseResult.success(organizer);
    }

    @PostMapping("/login")
    public ResponseResult<Organizer> login(@RequestParam Integer organizerId,
                                           @RequestParam String password) {
        return organizerService.login(organizerId, password)
                .map(organizer -> {
                    organizer.setPassword(null); // 不返回密码
                    return ResponseResult.success(organizer);
                })
                .orElse(ResponseResult.error("Login failed"));
    }
    @GetMapping("/findById/{organizerId}")
    public ResponseResult<Organizer> findById(@PathVariable Integer organizerId) {
        Organizer organizer = organizerService.getOrganizerById(organizerId);
        if (organizer != null) {
            organizer.setPassword(null); // 不返回密码
            return ResponseResult.success(organizer);
        }
        return ResponseResult.error("Organizer not found");
    }
    @PostMapping("/loginByname")
    public ResponseResult<Organizer> loginByName(@RequestParam String organizerName,
                                                 @RequestParam String password) {
        return organizerRepository.findByOrganizerName(organizerName)
                .filter(o -> PasswordUtil.matches(password, o.getPassword()))
                .map(organizer -> {
                    organizer.setPassword(null);
                    return ResponseResult.success(organizer);
                })
                .orElse(ResponseResult.error("Login failed"));
    }

// OrganizerController.java 添加以下内容

    @GetMapping("/registrations")
    public ResponseResult<List<Registration>> getRegistrations(@RequestParam Integer organizerId) {
        return ResponseResult.success(organizerService.getRegistrationsByOrganizer(organizerId));
    }

    @PutMapping("/registration/{registrationId}/approve")
    public ResponseResult<Registration> approveRegistration(@PathVariable Integer registrationId) {
        return ResponseResult.success(organizerService.updateRegistrationStatus(registrationId, "approved"));
    }

    @PutMapping("/registration/{registrationId}/reject")
    public ResponseResult<Registration> rejectRegistration(@PathVariable Integer registrationId) {
        return ResponseResult.success(organizerService.updateRegistrationStatus(registrationId, "rejected"));
    }
    @PostMapping("/publishEvent")
    public ResponseResult<Event> publishEvent(@RequestBody Event event,
                                              @RequestParam Integer organizerId) {
        try {
            // 设置默认状态
            event.setStatus("upcoming");
            // 设置主办方
            Organizer organizer = new Organizer();
            organizer.setOrganizerId(organizerId);
            event.setOrganizer(organizer);

            Event savedEvent = organizerService.publishEvent(event);
            return ResponseResult.success(savedEvent);
        } catch (Exception e) {
            return ResponseResult.error("发布赛事失败: " + e.getMessage());
        }
    }



    @PostMapping("/updateInfo")
    public ResponseResult<Organizer> updateInfo(@RequestParam Integer organizerId,
                                                @RequestParam(required = false) String name,
                                                @RequestParam(required = false) String oldPassword,
                                                @RequestParam(required = false) String newPassword) {
        // 验证旧密码
        if (newPassword != null && !newPassword.isEmpty()) {
            Organizer organizer = organizerService.getOrganizerById(organizerId);
            if (organizer == null || !organizer.getPassword().equals(oldPassword)) {
                return ResponseResult.error("Old password is incorrect");
            }
        }
        return ResponseResult.success(organizerService.updateOrganizerInfo(organizerId, name, newPassword));
    }
}